<?php
/**
 * 
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月21日 上午10:42:33 
 * 类说明
 */
class AuthRuleAction extends BaseAction{
	/**
	 * @authority 浏览
	 */
	public function index(){
		$Model = M('Auth_rule');
		$map['status'] = 1;
		$map['type'] = 1;
		$vo = $Model->where($map)->order('seq asc')->select();
		//查询出层级关系数据
		for($i=0; $i<count($vo); $i++) {
			$map['type'] = 2;
			$map['parent_id'] = $vo[$i]['id'];
			$sub_menu = $Model->where($map)->order('seq asc')->select();
			$vo[$i]['sub_menu'] = $sub_menu;
			for($j=0; $j<count($sub_menu); $j++) {
				$map['type'] = 3;
				$map['parent_id'] = $sub_menu[$j]['id'];
				$sub_method = $Model->where($map)->select();
				$vo[$i]['sub_menu'][$j]['sub_method'] = $sub_method;
			}
		}
		$this->assign('vo',$vo);
		$this->display('index');
	}
	
	/**
	 * 插入主菜单
	 */
	public function insert(){
		$Model = M($this->getActionName());
		unset ( $_POST[$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		$data['module'] = GROUP_NAME;
		//查询最大排序
		$map['status'] = 1;
		$map['type'] = 1;
		$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
		$data['seq'] = $max_seq + 1;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	/**
	 * @authority 新增子菜单
	 */
	public function addSubMenu(){
		$parent_id = $_REQUEST["parent_id"];
		$this->assign('parent_id',$parent_id);
		$this->display('addSubMenu');
	}
	/**
	 * 插入子菜单
	 */
	public function insertSubMenu(){
		$Model = M($this->getActionName());
		unset ( $_POST[$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		$data['module'] = GROUP_NAME;
		if(strpos($data['url'], ".")) {
			$name = substr($data['url'],0,strrpos($data['url'],'.'));
		}elseif (strpos($data['url'], "?")){
			$name = substr($data['url'],0,strrpos($data['url'],'?'));
		}else{
			$name = $data['url'];
		}
		$data['name'] = GROUP_NAME."/".$name;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	/**
	 * @authority 修改子菜单
	 */
	public function editSubMenu(){
		$Model = M($this->getActionName());
		$map['id'] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$this->assign('vo',$vo);
		$this->display('editSubMenu');
	}
	
	
	/**
	 * @authority 修改排序
	 */
	public function editSeq() {
		$Model = M($this->getActionName());
		$map['status'] = 1;
		$map['type'] = 1;
		$vo = $Model->where($map)->field("id,title")->order('seq asc')->select();
		$map['type'] = 2;
		$sub_menu = $Model->where($map)->field("id,title,parent_id")->order('seq asc')->select();
		$newData = array();
		$k = 0;
		for($i=0; $i<count($vo); $i++) {
			$newData[$k]['id'] = $vo[$i]['id']; 
			$newData[$k]['name'] = $vo[$i]['title'];
			$newData[$k]['pId'] = 0;
			$newData[$k]['isParent'] = true;
			$newData[$k]['open'] = true;
			$k++;
			for($j=0; $j<count($sub_menu); $j++) {
				if($vo[$i]['id'] == $sub_menu[$j]['parent_id']) {
					$newData[$k]['id'] = $sub_menu[$j]['id'];
					$newData[$k]['pId'] = $vo[$i]['id'];
					$newData[$k]['name'] = $sub_menu[$j]['title'];
					$k++;
				}
			}
		}
		$this->assign("zNodes",json_encode($newData));
		$this->display();
	}
	
	//更新排序
	public function updateSeq(){
		$Model = M($this->getActionName());
		//批量更新
		$seq = $_REQUEST['seq'];
		$sql .= 'SET seq = CASE id ';
		foreach ($seq as $key=>$val){
			$ids .= $val.",";
			$key++;
			$sql .= ' WHEN '.$val.' THEN '.$key." ";
		}
		$sql .= ' END ';
		$ids = substr($ids,0,strlen($ids)-1);
		$sql .= 'where id in ('.$ids.')';
		$where['id'] = array('in',$ids);
		$data['_query'] = $sql;
		$result = $Model->execute('update __TABLE__ '.$sql);
		if($result !== false){
			$this->ajaxReturn($Model->getLastSql(),L('ADMIN_SEQ_SUCCESS'),1);
		}else{
			$this->ajaxReturn($Model->getLastSql(),L('ADMIN_SEQ_ERROR'),0);
		}
	}
	
	
	/**
	 * 获取所有含authority的方法和注释
	 * 不排除父类
	 */
	public function cc_get_methods($action){
		$mets = get_class_methods($action);
		$newMet = array();
		$act   = new $action();
		foreach ($mets as $key=>$val){
			$func  = new ReflectionMethod($act,$val);//反射方法
			$tmp   = $func->getDocComment();
			$flag  = preg_match_all('/@authority(.*?)\n/',$tmp,$tmp);
			$tmp   = trim($tmp[1][0]);
			if($tmp != '' && !empty($tmp)){
				$data['name'] = $val;
				$data['authority'] = $tmp;
				$newMet[] = $data;
			}
		}
		return $newMet;
	}
	
	/**
	 * 获取类下面所有方法和注释
	 * 排除父类
	 */
	function get_methods($action){
		$reAct = new reflectionclass($action);//反射类
		$methods = $reAct->getMethods();
		$newMet = array();
		$act   = new $action();
		foreach ($methods as $key=>$val){
			if($val->class == $action){
				$data['name'] = $val->name;
				$data['class'] = $val->class;
				$func  = new ReflectionMethod($act,$val->name);//反射方法
				$tmp   = $func->getDocComment();
				$flag  = preg_match_all('/@authority(.*?)\n/',$tmp,$tmp);
				$tmp   = trim($tmp[1][0]);
				$tmp   = $tmp !='' ? $tmp:'无';
				$data['cc'] = $tmp;
				$newMet[] = $data;
			}
		}
		return $newMet;
	}
	
}